<?php

// +----------------------------------------------------------------------
// | ThinkCMF [ WE CAN DO IT MORE SIMPLE ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013-2019 http://www.thinkcmf.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: 老猫 <thinkcmf@126.com>
// +----------------------------------------------------------------------

namespace app\admin\controller;

use app\admin\model\ThemeModel;
use cmf\controller\AdminBaseController;
use think\Db;
use cmf\lib\Upload;
use think\facade\View;

class UploadController extends AdminBaseController {
    
    
    
    public function index(){
        
        $assign_data = array();
        $token = md5(time());
        $assign_data = array(
            'token'=>$token
        );
        
        $this->assign($assign_data);
        
        
        return $this->fetch();
    }

    public function swfupload() {
         $request_path = $this->request->param('path');
        if ($this->request->isPost()) {                       
            $uploader = new Upload();

            $result = $uploader->upload();

            if ($result === false) {
                $code = 0;
                $this->error($uploader->getError());
            } else {
                $code = 1;
//                $filesize = $this->trans_byte(filesize('.'.$result['url']));
                $result['filesize'] = '';
                $result['name'] = empty($request_path)?'':$request_path;
            }
            
        }
        return json($result = [
            'code' => $code,
            'msg'  => '上传成功',
            'data' => $result,
            'url'  => "",
            'wait' => "",
        ]);
    }
    
    
    
    public function base64ToImage(){
        $request_data = $this->request->param();
        $base64_image_content = $request_data['photo_base64'];
        $code = 0;
        if($this->request->isPost() && !empty($base64_image_content)){
            
            
            if (preg_match('/^(data:\s*image\/(\w+);base64,)/', $base64_image_content, $result)){
                
                $strDate    = date('Ymd');
                $app = $this->request->post('app');
                if (empty($app) || !file_exists(APP_PATH . $app)) {
                    $app = 'default';
                }
                $strFileExtension = $result[2];
                
                $fileSaveName    = (empty($app) ? '' : $app . '/') . $strDate . '/' . md5(uniqid()) . "." . $strFileExtension;
                $strSaveFilePath = './upload/' . $fileSaveName; //TODO 测试 windows 下
                $strSaveFileDir  = dirname($strSaveFilePath);
                
                if(!file_exists($strSaveFileDir)){
                    //检查是否有该文件夹，如果没有就创建，并给予最高权限
                    mkdir($strSaveFileDir, 0700);
                }
                if (file_put_contents($strSaveFilePath, base64_decode(str_replace($result[1], '', $base64_image_content)))){
                    $code = 1;
                    $path = $request_data['path'];
                    $filesize = $this->trans_byte(filesize($strSaveFilePath));
                    
                    
                }
            }
        }
        
         $filename = $request_data['token'];
        
        $str = '文件名:' . $request_data['path'] . '; 状态:'.($code == 1 ? 'Success' :'Fail') ."\r\n";
        $this->recordFail($str,$filename);

        return json($result = [
            'code' => $code,
            'msg'  => '上传成功',
            'data' => array(
                'filepath'=> empty($fileSaveName)?'':$fileSaveName,
                'filesize'=>empty($filesize)?'':$filesize,
                'name'=>empty($path)?'':$path,
            ),
            'url'  => "",
            'wait' => "",
        ]);
       
    }
    
   public function recordFail($str,$filename){
        if(empty($str)) return false;
       
        $dir = APP_PATH .'upload/data/imagelog/'.date('Ymd') .'/';
        if(!is_dir($dir)){
            @mkdir($dir, 0777,TRUE);
        }
        
        $file_path = $dir.$filename.'.log';
        
        $handle = fopen($file_path, 'a');
        fwrite($handle, $str);
        fclose($handle);
        
    }
    
    public function trans_byte($byte){

        $KB = 1024;
        $MB = 1024 * $KB;
        $GB = 1024 * $MB;
        $TB = 1024 * $GB;
        if ($byte < $KB) {
            return $byte . "B";
        } elseif ($byte < $MB) {
            return round($byte / $KB, 2) . "KB";
        } elseif ($byte < $GB) {
            return round($byte / $MB, 2) . "MB";
        } elseif ($byte < $TB) {
            return round($byte / $GB, 2) . "GB";
        } else {
            return round($byte / $TB, 2) . "TB";
        }
    }
        

}
	